package com.company.card_service.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.card_service.mapper.CompanyModelMapper;
import com.company.card_service.model.CompanyModel;
import com.company.card_service.service.CompanyModelService;
import com.company.card_service.vo.Result;
import com.company.card_service.vo.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CompanyModelServiceImpl extends ServiceImpl<CompanyModelMapper, CompanyModel> implements CompanyModelService {
    @Autowired
    private CompanyModelMapper companyModelMapper;

    @Override
    public CompanyModel getModel(Integer modelId) {
        CompanyModel companyModel = baseMapper.selectById(modelId);
        return companyModel;
    }

    @Override
    public Result configureCompanyModel(int cmId) {
        //1.通过模块id查找相应模块
        CompanyModel companyModel = companyModelMapper.selectById(cmId);
        if (companyModel != null) {
            UpdateWrapper<CompanyModel> wrapper = new UpdateWrapper<>();
            return Result.success(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(),companyModel);
        }else {
            return Result.fail(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage());
        }
    }
    @Override
    public Result configureShowCompanyModel(int cmId, String name, String url,int isShow) {
//1.通过模块id查找相应模块
        CompanyModel companyModel = companyModelMapper.selectById(cmId);
        if (companyModel != null) {
            UpdateWrapper<CompanyModel> wrapper = new UpdateWrapper<>();
            //2.改变is_show字段
            wrapper.eq("id", companyModel.getId());
            wrapper.set("is_show", companyModel.getIsShow());
            //3.修改模块名称
            wrapper.set("name", companyModel.getName());
            //4.修改模块图标
            wrapper.set("url", companyModel.getUrl());
            int i = companyModelMapper.update(companyModel, wrapper);

            if (i > 0) {
                return Result.success(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), companyModel);
            } else {
                return Result.fail(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage());
            }
        } else {
            return Result.fail(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage());
        }
    }
}
